﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace XbdSoft.WPFVersion.ViewModel
{
    public class ImageSourceConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = string.Empty;
            try
            {
                if (value!=null && typeof(string) == value.GetType())
                {
                    source = value.ToString();
                }
                if (String.IsNullOrEmpty(source))
                    source = "/Images/show_Ima.jpg";
                else
                    source = "http://www.xbd61.com/" + source;
                return new BitmapImage(new Uri(source, UriKind.RelativeOrAbsolute));
            }
            catch { }
            return new BitmapImage(new Uri("/Images/show_Ima.jpg",UriKind.RelativeOrAbsolute));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AgesConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                var ages = (value ?? string.Empty).ToString();
                var agesArray = ages.Split("|".ToArray());
                var result =new List<string>();
                if (agesArray.Contains("1"))
                    result.Add("胎儿");
                if (agesArray.Contains("2"))
                    result.Add("新生儿");
                if (agesArray.Contains("3"))
                    result.Add("0-1岁");
                if (agesArray.Contains("4"))
                    result.Add("1-2岁");
                if (agesArray.Contains("5"))
                    result.Add("2-3岁");
                if (agesArray.Contains("6"))
                    result.Add("3-4岁");
                if (agesArray.Contains("7"))
                    result.Add("4-6岁");
                if (agesArray.Contains("8"))
                    result.Add("6岁以上");
                var  dd= result.Aggregate(string.Empty, (current, val) => { return current + "|" + val; });
                return dd;
            }
            catch { }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
